<?php

namespace App\Model;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class DeviceThree extends Model
{
    use SoftDeletes;

    protected $table = 'devices_three';
    protected $fillable = ['user_id', 'site_id', 'device_id', 'group_id', 'name', 'sn', 'normal_power', 'actual_power','offset'];

    public function group()
    {
        return $this->belongsTo('App\Model\Group');
    }

    public static function getDevicesBySiteId($site_id)
    {
        $devices = DeviceThree::with('group')->where('site_id', $site_id)->get(['user_id','device_id']);
        return $devices;
    }

    public static function getDevicesCountByGroupId($group_id)
    {
        $count = DeviceThree::where('group_id', $group_id)->count();
        return $count;
    }

    public static function getDevicesByGroupId($group_id)
    {
        return DeviceThree::where('group_id', $group_id)->get();
    }

    public static function getDevicesUserId($user_id){
        $devices = DeviceThree::where('user_id', $user_id)->get();
        return $devices;
    }

    public static function getDevicesByUserId($user_id)
    {
        $devices = DeviceThree::with('group')->where('user_id', $user_id)->get();
        return $devices;
    }

    public static function getDevicesCountByUserId($user_id)
    {
        $count = DeviceThree::where('user_id', $user_id)->count();
        return $count;
    }

    public static function getDevicesCountBySiteId($site_id)
    {
        $count = DeviceThree::where('site_id', $site_id)->count();
        return $count;
    }

    public static function findByDeviceId($user_id,$device_id)
    {
        $device = DeviceThree::where('user_id',$user_id)->where('device_id', $device_id)->first();
        return $device;
    }

    /**
     * @param $user_id
     *
     * 只统计在线且无警报状态的机器
     *
     */
    public static function getWorkingDevicesCountByUserId($user_id)
    {
        $devices = DeviceThree::where('user_id', $user_id)->get();
        $count = 0;
        foreach ($devices as $device) {

            $ivd = InverterDataThree::getLastInverterDataByDeviceId($user_id, $device->device_id);


            //数据不存在则离线
            if ($ivd) {
                //最近一次数据为60分钟前则离线
                $now = time();
                $created_at = strtotime($ivd->created_at);
                if ($now - $created_at < 60 * 60) {
                    //最近一次非警报状态则正常运行
//                    if ($ivd->IVStatus <= 3 && $ivd->IVStatus > 0) {
                        $count++;
//                    }
                }
            }
        }
        return $count;
    }

    /**
     * @param $user_id
     * 在线且无警报状态的机器
     */
    public static function getWorkingDevicesByDeviceId($s,$user_id)
    {

//            $ivd = InverterDataThree::getLastInverterDataByDeviceId($user_id, $device_id);
        $ivd = json_decode($s);

        if($ivd)
        {
            $now = time();
            $EventTime = strtotime($ivd->EventTime);
            if ($now - $EventTime < 3600) {
                $count = Warn::where('site_id', $ivd->site_id)
                    ->where('device_id', $ivd->device_id)
                    ->where('user_id',$user_id)
                    ->where('read', 0)
                    ->where('invert_status', '<>', 0)
                    ->count();
                if ($count == 0) {
                    return 1;
                }else {
                    return 0;
                }
            }

        }
        return 0;
    }





    public static function getAllActualPowerByUserId($user_id)
    {
        $value = 0;
        $devices = DeviceThree::where('user_id', $user_id)->get();
        foreach ($devices as $device) {
            //actual_power  计算方法是 (总装机量/device的数量)*1000
            $value = $value + $device->actual_power;
        }
        return $value;
    }

}
